package com.cs.study.homework._0315;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Demo2 {
    public static void main(String[] args) {

        String str = "abcqweracb";
        String norepeat = removeRepeat(str);
        System.out.println(norepeat);

    }

    /**
     * 给定一个字符串, 去除这个字符串中重复的数值, 得到一个不重复的字符串
     *
     * @param str: 给定字符串
     * @return: 去除重复值的字符串
     */
    private static String removeRepeat(String str) {
        //将string转变为list
        String[] strings = str.split("");
        List<String> list = Arrays.asList(strings);
        List<String> newList = new ArrayList<>();
        for (String s : list) {
            if (!newList.contains(s)){
                newList.add(s);
            }
        }

        //利用StringBuffer将list转为string
        StringBuffer sb = new StringBuffer();
        for (String s : newList) {
            sb.append(s);
        }
        String s = sb.toString();

        return s;
    }
}
